home *** CD-ROM | disk | FTP | other *** search
- /********************************************************************/
- /* */
- /* THE POOP SHELL */
- /* */
- /********************************************************************/
- /* An Adventure in Pseudo-Object-Oriented-Programming */
- /********************************************************************/
- /*
- * >>> File name: Library
- *
- * >>> Purpose: A bunch of routines which don't fit elsewhere
- * >>> Project: POOPDraw
- * >>> Date: 6/8/89
- * >>> By: Adam Treister
- *
- */
- /********************************************************************/
- /* For Your Information 1802 Hillside Rd. SB CA 93101 */
- /********************************************************************/
- #include "PoopDrawInc"
-
-
- #define TYPE 'DWNG' /* data file type id */
- #define CREATOR 'POOP'
- #define FILETYPE 'DWNG'
- static Point where = {90,90};
- SFTypeList myTypes = { TYPE, '\0', '\0', '\0'};
-
- /********************************************************************/
- /*------------------------------------------------------------------*/
-
- void InitMacintosh()
- {
- MaxApplZone();
- MoreMasters(); MoreMasters();
- MoreMasters(); MoreMasters();
- MoreMasters(); MoreMasters();
- InitGraf(&thePort); /* Initialize the Macintosh Toolbox Manager */
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL); /* No Resume Procedure */
- InitCursor();
- }
-
- /*---------------------------------------- TESetString */
- void TESetString(teH,str)
- TEHandle teH;
- Str255 str;
- {
- TESetText(&str[1],(long) str[0], teH);
- }
- /*---------------------------------------- TEGetString */
- void TEGetString(teH,str)
- TEHandle teH;
- Str255 str;
- {
- register int i,len;
- register char *hText;
-
- str[0] = len = MIN(255,(*teH)->teLength);
- hText = *((*teH)->hText);
- for (i = 1; i <= len; i++) str[i] = hText[i-1];
- }
-
- /*------------------------------------------------ DrawLine -------*/
-
- void DrawLine(a,b)
- Point a,b;
- { MoveTo(a.h,a.v);
- LineTo(b.h,b.v);
- }
- /*---------------------------------------- DrawPic */
-
- void DrawPic(x,y,picture)
- int x,y;
- PicHandle picture;
- { Rect destRect;
-
- SetRect(&destRect,x,y,
- x + Width((**picture).picFrame),
- y + Height((**picture).picFrame));
- DrawPicture(picture,&destRect);
- }
- /*----------------------------------------------------- Width */
-
- Width(r)
- Rect r;
- { return(r.right - r.left); }
-
- /*------------------------------------------------------ Height */
- Height(r)
- Rect r;
- { return(r.bottom - r.top); }
-
- /*------------------------------------------------------- SortRect -*/
-
- void SortRect(rP)
- Rect *rP;
- {
- int temp;
-
- if (rP->left > rP->right) {temp = rP->left; rP->left = rP->right; rP->right = temp;}
- if (rP->top > rP->bottom) {temp = rP->top; rP->top = rP->bottom; rP->bottom = temp;}
-
- }
- /*------------------------------------------------------------------*/
-
- void TurnWatchOn()
- { SetCursor(*(GetCursor(4)));
- }
- /*------------------------------------------------------------------*/
-
- void TurnArrowOn()
- { SetCursor(&arrow);
- }
-
- /*---------------------------------------- GetDragRect */
- void GetDragRect(rp)
- Rect *rp;
- { *rp = screenBits.bounds; }
- /*------------------------------------------------------ OurWindow */
-
- Boolean OurWindow(wP)
- WindowPeek wP;
- {
- if (!wP) return(FALSE);
- return (wP->windowKind > 0);
- }
-
- /* ------------------------------------------------------------ */
- /* */
- /* GET NEW HANDLE */
- /* */
- /* ------------------------------------------------------------ */
-
- Handle GetNewHandle(hsize)
- long hsize;
- {
- Handle h;
-
- h = NewHandle(hsize); MEM_CHECK
- return(h);
- }
-
- /* ------------------------------------------------------------ */
- /* */
- /* DisposeHandle */
- /* */
- /* ------------------------------------------------------------ */
-
- void DisposeHandle(h)
- Handle h;
-
- {
- if (!h) return;
- DisposHandle(h); MEM_CHECK
- }
- /*----------------------------------------------------------------
- *
- * NullOutHandle(h)
- *
- * May 25, 1988 created AST
- *
- * -- never has been thoroughly tested!!!!
- * -- there is also a better way to do it, by telling the memory
- * manager to clear the memory when it allocates it
- *----------------------------------------------------------------*/
- void NullOutHandle(h)
- Handle h;
- {
- NullOutBytes(*h,GetHandleSize(h));
- }
- /*----------------------------------------------------------------
- *
- * NullOutBytes(p,siz)
- *
- * May 25, 1988 created AST
- * March 8,1989 spawned from NullOutHandle AST
- *----------------------------------------------------------------*/
- void NullOutBytes(p,siz)
- Ptr p;
- long siz;
- {
- while (siz--) *p++ = (Byte) 0;
- }
- /* ------------------------------------------------- Oops ----------- */
-
-
- void Oops(s, num, Recoverable)
- char *s;
- int num;
- Boolean Recoverable;
-
- {
- Str255 numStr,temp;
-
- NumToString((long) num, &numStr);
- *temp=0;
- PStrCat (4,temp, s,"\p ", numStr);
-
- DebugStr(temp);
- if (!Recoverable) ExitToShell();
- }
- /*---------------------------------------------------------PStrCat---*/
-
-
- PStrCat(count, dst)
- register int count; /* # of strings (including dst) */
- unsigned char *dst; /* destination pascal string */
- {
- register unsigned char *dstPtr, *srcPtr;
- register unsigned char **argList = &dst;
- register int argLen, totLen;
-
- if ((totLen = *dst) < 255) {
- if (count > 30)
- count = 30; /* max. # of string args is 30 */
- dstPtr = dst + totLen; /* dstPtr = 1 past end of dst */
- while (--count > 0 && totLen < 255) {
- srcPtr = *++argList;
- argLen = srcPtr[0];
- if (totLen + argLen > 255)
- argLen = 255 - totLen; /* max totLen = 255 */
- totLen += argLen;
- while (--argLen >= 0) /* add arg's char to dst */
- *++dstPtr = *++srcPtr;
- }
- dst[0] = totLen; /* sets length of dst */
- }
- }
-
- /*-------------------------------------------------------StrCopy----*/
- StrCopy(a,b)
- Str255 a,b;
-
- { int len = *b = *a;
-
- while (len-- >= 0) *b++ = *a++;
- }
-
- /*------------------------------------------------------------------*/
-
- void WriteHandleToFile(h,replyP)
- Handle h;
- SFReply *replyP;
- {
- long nBytes;
- int volSave,dataFork;
- Str255 s;
-
- TurnWatchOn();
- GetVol(&s,&volSave);
- SetVol(NULL,replyP->vRefNum);
-
- FSDelete(replyP->fName,replyP->vRefNum); /* Trash old file version */
-
- FILE_CHECK(Create(replyP->fName,replyP->vRefNum,CREATOR,FILETYPE));
- FILE_CHECK(FSOpen(replyP->fName,replyP->vRefNum,&dataFork));
- nBytes = GetHandleSize(h);
- FILE_CHECK(FSWrite(dataFork,&nBytes,*h));
- if (nBytes != GetHandleSize(h))
- Oops("\pnBytes Error in save",nBytes,FALSE);
-
- FILE_CHECK(FSClose(dataFork));
-
- SetVol(NULL,volSave);
- TurnArrowOn();
- }
- /*------------------------------------------------------------------*/
- Handle ReadFileToHandle()
- {
- SFReply reply;
- long fSize;
- int dataFork;
- Handle h;
-
- SFGetFile( where, "\pOpen the document:", 0L, 1, myTypes, 0L, &reply );
- if (reply.good)
- {
- FILE_CHECK(FSOpen(reply.fName,reply.vRefNum,&dataFork));
- GetEOF(dataFork,&fSize);
- h = GetNewHandle(fSize);
- FILE_CHECK(FSRead(dataFork,&fSize,*h));
- FILE_CHECK(FSClose(dataFork));
- return(h);
- }
- else return(NULL);
- }
- /*------------------------------------------------------------------*/
- Rect GetWindowRect(wP) /* there MUST be a more direct way to do this !! */
- WindowPtr wP;
- {
- Point topleft;
- Rect wRect;
-
- SetPt(&topleft,0,0);
- LocalToGlobal(&topleft);
- SetRect(&wRect,topleft.h,topleft.v,
- topleft.h+Width(wP->portRect),topleft.v + Height(wP->portRect));
- return(wRect);
- }
- /*------------------------------------------------------------------*/
- WindowPtr MyFrontWindow()
- {
- WindowPeek wP;
-
- wP = FrontWindow();
- while (wP AND wP->windowKind < 0)
- wP = wP->nextWindow;
- return(wP);
- }